package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.account.client.RechargeInfoFeignClient;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.jsapi.model.CloseOrderRequest;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private RechargeInfoFeignClient rechargeInfoFeignClient;

    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;

    @Autowired
    private KafkaService kafkaService;


    @Override
    public boolean closePayment(String orderNo) {
        //  创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_CANCEL);
        //  设置更新条件
        boolean result = this.update(paymentInfo, new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
        //  返回
        return result;
    }

    @Override
    public void updatePaymentStatus(Transaction result) {
        //  修改交易记录状态。
        //  创建对象
        PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo,result.getOutTradeNo()));
        if (null != paymentInfo && paymentInfo.getPaymentStatus().equals(SystemConstant.PAYMENT_STATUS_UNPAID)){
            //  微信交易号
            paymentInfo.setOutTradeNo(result.getTransactionId());
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
            paymentInfo.setCallbackContent(result.toString());
            paymentInfo.setCallbackTime(new Date());
            //  修改完成.
            this.updateById(paymentInfo);
            //  发送主体
            String topic = paymentInfo.getPaymentType().equals(SystemConstant.PAYMENT_TYPE_ORDER)? KafkaConstant.QUEUE_ORDER_PAY_SUCCESS:KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS;
            //  发送消息.
            kafkaService.sendMsg(topic,paymentInfo.getOrderNo());
        }
    }

    @Override
    public PaymentInfo savePaymentInfo(OrderInfo orderInfo, String paymentType, String orderNo) {
        //  防止同一个订单号同一个业务有多条记录.
        PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getPaymentType, paymentType).eq(PaymentInfo::getOrderNo, orderInfo));
        if (null != paymentInfo){
            return paymentInfo;
        }
        //  创建对象
        paymentInfo = new PaymentInfo();
        //  赋值：
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        //  判断：
        if (paymentType.equals(SystemConstant.PAYMENT_TYPE_ORDER)){
            //  订单支付
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPayWay(orderInfo.getPayWay());
            paymentInfo.setAmount(orderInfo.getOrderAmount());
            paymentInfo.setContent("微信支付订单");
        } else {
            //  充值支付
            Result<RechargeInfo> rechargeInfoResult = rechargeInfoFeignClient.getRechargeInfo(orderNo);
            Assert.notNull(rechargeInfoResult,"充值结果集为空");
            RechargeInfo rechargeInfo = rechargeInfoResult.getData();
            Assert.notNull(rechargeInfo,"充值对象为空");
            paymentInfo.setUserId(rechargeInfo.getUserId());
            paymentInfo.setPayWay(rechargeInfo.getPayWay());
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
            paymentInfo.setContent("微信充值");
        }

        //  保存
        this.save(paymentInfo);
        //  返回数据.
        return paymentInfo;
    }
}
